//到货通知单
$(function(){
	//更新行数据
	function otherInboundInfoUpdateActions(value){
		$.post(otherInboundInfoEditUrl,{
									id			:value.id,
									unit_count	:value.unit_count,
									price		:value.price,
									conforming_p:value.conforming_p,
									nonconforming_p:value.nonconforming_p,
									scrap_one	:value.scrap_one
								},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#otherInboundInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#otherInboundInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#otherInboundInfoDatagrid').datagrid('selectRow', index);
				$('#otherInboundInfoDatagrid').datagrid('endEdit', editIndex);
				$('#otherInboundInfoDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#otherInboundInfoDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#otherInboundInfoDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getotherInboundInfoUrl,
		title:'其它入库单详情',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'unit1',title:'单位',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.unit_name;
				}
		},
		{field:'unit_count',title:'单位数量',sortable:true,
				editor: {
					type: 'numberbox',  
					options: {
						required: true,  
						missingMessage: '不能为空',
						min:1
					}
				}
		},
		{field:'price',title:'单价',sortable:true,
				editor: {
					type: 'numberbox',  
					options: {
						required: true,  
						missingMessage: '不能为空',
						min:0
					}
				}
		},
		// {field:'conforming_p',title:'合格品',sortable:true,
				// editor: {
					// type: 'numberbox',  
					// options: {
						// required: true,  
						// missingMessage: '不能为空',
						// min:0
					// }
				// }
		// },
		// {field:'nonconforming_p',title:'残次品',sortable:true,
				// editor: {
					// type: 'numberbox',  
					// options: {
						// required: true,  
						// missingMessage: '不能为空',
						// min:0
					// }
				// }
		// },
		// {field:'scrap_one',title:'报废品',sortable:true,
				// editor: {
					// type: 'numberbox',  
					// options: {
						// required: true,  
						// missingMessage: '不能为空',
						// min:0
					// }
				// }
		// },
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称',sortable:true},
		{field:'goods_spec',title:'规格',sortable:true},
		{field:'current_type',title:'币制',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCoinUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.coin_name;
				}
		},
		{field:'source',title:'原产国',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCountryUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.country_name;
				}
		}
		]],
		queryParams:{
			order_id:order_id
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					otherInboundInfoUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
otherInboundInfoObj={
	save:function(url){//编辑后保存数据
		$('#otherInboundInfoEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#otherInboundInfoEditDialog').dialog('close');
                    });
                }else{
                    $('#otherInboundInfoEditDialog').dialog('close');      
                    $('#otherInboundInfoDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#otherInboundInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#otherInboundInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#otherInboundInfoDatagrid').datagrid('acceptChanges');
		}
	},
	addinfoBar:function(){//添加商品
        window.parent.showTab(booksInfoUrl,'账册详情','',order_id);
	},
	reloadBar:function(){//刷新数据
		$('#otherInboundInfoDatagrid').datagrid('reload');
	},
	editBar:function(){//显示编辑框
		var otherInboundInfoRow=$('#otherInboundInfoDatagrid').datagrid('getSelections');
        if (otherInboundInfoRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(otherInboundInfoRow.length==1){            	
        	$('#otherInboundInfoEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#otherInboundInfoEditForm').form('load',otherInboundInfoRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var otherInboundInfoRow=$('#otherInboundInfoDatagrid').datagrid('getSelections');
        if (otherInboundInfoRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+otherInboundInfoRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<otherInboundInfoRow.length;i++){
                		ids.push(otherInboundInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(otherInboundInfoDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#otherInboundInfoAddDialog').dialog('close');       
								$('#otherInboundInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}